#include "lvgl_init.h"
#include "esp_err.h"
#include "esp_log.h"

static void anim_x_cb(void * var, int32_t v)
{
    lv_obj_set_x(var, v);
}

static void anim_size_cb(void * var, int32_t v)
{
    lv_obj_set_size(var, v, v);
}

/**
 * Create a playback animation
 */
void lv_example_anim_2(void)
{

    lv_obj_t * obj = lv_obj_create(lv_scr_act());
    lv_obj_set_style_bg_color(obj, lv_palette_main(LV_PALETTE_RED), 0);
    lv_obj_set_style_radius(obj, LV_RADIUS_CIRCLE, 0);

    lv_obj_align(obj, LV_ALIGN_LEFT_MID, 10, 0);

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_values(&a, 10, 70);             // 设置起始位置
    lv_anim_set_time(&a, 1000);                 // 设置时间
    lv_anim_set_playback_delay(&a, 100);
    lv_anim_set_playback_time(&a, 300);
    lv_anim_set_repeat_delay(&a, 500);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_path_cb(&a, lv_anim_path_ease_in_out);

    lv_anim_set_exec_cb(&a, anim_size_cb);
    lv_anim_start(&a);
    lv_anim_set_exec_cb(&a, anim_x_cb);
    lv_anim_set_values(&a, 10, 100);
    lv_anim_start(&a);
}

static void anim_roller(void * var, int32_t v)
{
    lv_roller_set_selected(var, v, LV_ANIM_ON);
}

static const char* TAG = "lvgl_fs";
static lv_fs_file_t f;
static lv_fs_dir_t d;
static char write_buf[10] = "123456789";

/*
wink.png
bulb.gif
small_image.sjpg
esp_fail.jpg
test.png
*/

void lv_example_roller_1(void)
{
    lv_fs_res_t res;

    // 打开文件
    res = lv_fs_dir_open(&d, "S:/image");
    //res = lv_fs_dir_open(&d, "S:./");

    if(res != LV_FS_RES_OK)
    {
        ESP_LOGI(TAG,"Open error");
        printf("%d\n", res);
    }

    char fn[256];
    while(1) {
        res = lv_fs_dir_read(&d, fn);
        if(res != LV_FS_RES_OK) {
            break;
        }

        /*fn is empty, if not more files to read*/
        if(strlen(fn) == 0) {
            break;
        }

        printf("%s\n", fn);
    }

    lv_fs_dir_close(&d);
#if 0
    lv_obj_t * img;

    // img = lv_img_create(lv_scr_act());
    // lv_img_set_src(img, &img_wink_png);
    // lv_obj_align(img, LV_ALIGN_LEFT_MID, 20, 0);

    img = lv_gif_create(lv_scr_act());
    /* Assuming a File system is attached to letter 'A'
     * E.g. set LV_USE_FS_STDIO 'A' in lv_conf.h */
    lv_gif_set_src(img, "S:/image/bulb.gif");
    lv_obj_align(img, LV_ALIGN_RIGHT_MID, -20, 0);

    img = lv_img_create(lv_scr_act());
    /* Assuming a File system is attached to letter 'A'
     * E.g. set LV_USE_FS_STDIO 'A' in lv_conf.h */
    lv_img_set_src(img, "S:/image/test1.png");
    lv_obj_align(img, LV_ALIGN_CENTER, -20, 0);
#endif
#if 0
    //lv_fs_fatfs_init();

    lv_fs_res_t res;

    // 打开文件
    res = lv_fs_open(&f, "S:/file.txt", LV_FS_MODE_WR);
    //res = lv_fs_dir_open(&d, "S:./");

    if(res != LV_FS_RES_OK)
    {
        ESP_LOGI(TAG,"Open error");
        printf("%d\n", res);
    }

    // 写入内容
    res = lv_fs_write(&f, write_buf, (sizeof(write_buf) / sizeof(char)), NULL);
    if(res != LV_FS_RES_OK)
    {
        ESP_LOGI(TAG,"Write error");
        printf("%d\n", res);
    }

    lv_fs_close(&f);

    // 打开文件
    res = lv_fs_open(&f, "S:/file.txt", LV_FS_MODE_RD);
    if(res != LV_FS_RES_OK)
    {
        ESP_LOGI(TAG,"Open error");
        printf("%d\n", res);
    }


    uint8_t buf[32];
    res = lv_fs_read(&f, buf, 8, NULL);

    if(res != LV_FS_RES_OK)
    {
        ESP_LOGI(TAG,"Read error");
        printf("%d\n", res);
    }
    printf("read %s\n", buf);

    lv_fs_close(&f);
#endif

#if 0
    const char * opts = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9";

    lv_obj_t *roller1 = lv_roller_create(lv_scr_act());
    lv_roller_set_options(roller1,
                        opts,
                        LV_ROLLER_MODE_INFINITE);
    //lv_obj_center(roller1);
    lv_roller_set_visible_row_count(roller1, 2);
    lv_obj_align(roller1, LV_ALIGN_CENTER, -90, 0);
    

    lv_obj_t *roller2 = lv_roller_create(lv_scr_act());
    lv_roller_set_options(roller2,
                        opts,
                        LV_ROLLER_MODE_INFINITE);
    //lv_obj_center(roller1);
    lv_roller_set_visible_row_count(roller2, 2);
    lv_obj_align(roller2, LV_ALIGN_CENTER, -45, 0);

    lv_obj_t *roller3 = lv_roller_create(lv_scr_act());
    lv_roller_set_options(roller3,
                        opts,
                        LV_ROLLER_MODE_INFINITE);
    //lv_obj_center(roller1);
    lv_roller_set_visible_row_count(roller3, 2);
    lv_obj_align(roller3, LV_ALIGN_CENTER, 0, 0);
    

    lv_obj_t *roller4 = lv_roller_create(lv_scr_act());
    lv_roller_set_options(roller4,
                        opts,
                        LV_ROLLER_MODE_INFINITE);
    //lv_obj_center(roller1);
    lv_roller_set_visible_row_count(roller4, 2);
    lv_obj_align(roller4, LV_ALIGN_CENTER, 45, 0);

    lv_anim_t a;
    lv_anim_init(&a);

    lv_anim_set_exec_cb(&a, anim_roller); 
    lv_anim_set_var(&a, roller1); 

    lv_anim_set_time(&a, 1000 * 10);

    lv_anim_set_values(&a, 0, 9);

    lv_anim_start(&a);
#endif
}






